package com.guet.config;

import com.sun.corba.se.spi.resolver.LocalResolver;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author MicahYin
 * @date 2020/8/4 17:59
 */
//配置自定义的语言转换器,国际化组件
public class MyLocaleResolver implements LocaleResolver{

//    解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //获取请求中的语言参数
        String language = httpServletRequest.getParameter("l");
        //如果没有指定的，则使用默认的配置
        Locale locale=Locale.getDefault();
        //如果请求的链接中带了国际化参数
        if(!StringUtils.isEmpty(language)){
            String[] split=language.split("_");
            //传入国家和地区
            locale=new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, @Nullable HttpServletResponse httpServletResponse, @Nullable Locale locale) {

    }
}
